package cn.ekuma.epos.analysis.front;
import java.awt.BorderLayout;

import javax.swing.ListSelectionModel;

import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.table.model.AbstractTableModelWapper;
import org.jdesktop.swingx.table.model.CalcRowTableModelWapper;
import org.jdesktop.swingx.table.model.calc.DoubleSum;

import com.openbravo.bean.erp.viewbean.PassengerFlowAnalysisInfo;
import com.openbravo.data.basic.BasicException;

import cn.ekuma.epos.analysis.front.model.PassengerFlowAnalysisTableModel;
import cn.ekuma.util.SwingUtil;

import javax.swing.JScrollPane;

public class JPassengerFlowTableView extends javax.swing.JPanel{
	private JXTable jXTable1;
	AnalysisModelManager manager;
	JPassengerFlowAnalysisPanel analysisPanel;
	AbstractTableModelWapper wapperModel;
	
	public JPassengerFlowTableView(JPassengerFlowAnalysisPanel analysisPanel,AnalysisModelManager manager) {
		this.analysisPanel=analysisPanel;
		this.manager=manager;
		initComponents();
	}
	
	private void initComponents() {
		setLayout(new BorderLayout(0, 0));	
		 JScrollPane scrollPane = new JScrollPane();
	     add(scrollPane, BorderLayout.CENTER);
	     jXTable1 = new org.jdesktop.swingx.JXTable();
	     wapperModel=new CalcRowTableModelWapper(manager.getTableModel(),4,new DoubleSum());
	     jXTable1.setModel(wapperModel);
	     jXTable1.setColumnControlVisible(true);
	     jXTable1.setAutoCreateRowSorter(true);
	     jXTable1.setAutoResizeMode(JXTable.AUTO_RESIZE_OFF);
	     jXTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	     jXTable1.addMouseListener(new java.awt.event.MouseAdapter() {
	            public void mouseClicked(java.awt.event.MouseEvent evt) {
	                jXTable1MouseClicked(evt);
	            }
	        });
	     scrollPane.setViewportView(jXTable1);
	}

	public void peferredJXTableColumn() {
		SwingUtil.PreferredJXTableColumn(jXTable1);
	}
	
	private void jXTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jXTable1MouseClicked
        if(evt.getClickCount()==2){
            int index=jXTable1.getSelectedRow();
              if(index!=-1){
                  index=jXTable1.convertRowIndexToModel(index);
                  index=wapperModel.getRealRowNumber(index);
                  if(index==-1)
                	  return;
                  PassengerFlowAnalysisTableModel tableModel=(PassengerFlowAnalysisTableModel)manager.getTableModel();
                  PassengerFlowAnalysisInfo prodAnalysis=tableModel.get(index);
                  try {
					manager.fireProductChanged(prodAnalysis);
					analysisPanel.showDialy();
				} catch (BasicException e) {
					e.printStackTrace();
				}  
             }
        }
    }//GEN-LAST:event_jXTable1MouseClicked
}
